/**
 * Project: TacticaPescaditos
 * File: SupportAttacker.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TacticaPescaditos;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TeamPlayer;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.geometrics.Vector2D;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: SupportAttacker
 */
public class SupportAttacker implements State<TeamPlayer> {
	
	private String name;

	/**
	 * 
	 */
	private SupportAttacker() {
		this.name = "SupportAttacker";
	}

	public static final State<TeamPlayer> instance = new SupportAttacker();

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#enter(java.lang.Object)
	 */
	public void enter(TeamPlayer player) {
		
		  player.getSteering().seekOn();
		  player.getSteering().setTarget(player.getTactica().getSupportSpot());
		
	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#execute(java.lang.Object)
	 */
	public void execute(TeamPlayer player) {
		TacticaPescaditos tactica = player.getTactica();
		if (!tactica.isControlling()||!tactica.getControllingPlayer().canKick()) {			
			player.getStateMachine().changeState(Wait.instance); 
			return;
		} 		
		

		Vector2D pos = tactica.getSupportSpot();

		if ( !pos.equals(player.getSteering().getTarget()) ) {    
			player.getSteering().setTarget(pos);
			player.getSteering().seekOn();
			return;
		}

		pos = tactica.getGoalTarget(player.getPosition(), player.getKickForce(),TacticaPescaditos.SHOOT_ANGLE);

		if(  pos != null) {
			tactica.requestPass(player);
			return;
		}


		if (player.isAtTarget()) {
			player.getSteering().seekOff();



			player.setVelocity(new Vector2D(0,0));

			if (!player.isThreatened()){
				tactica.requestPass(player);				
			} 
			return;
		}
		
		 if (player.getBall().getSpeed()<1){
			 if (player.isClosestToBall()) {
				player.getStateMachine().changeState(ChaseBall.instance);
			 } else {
				 player.getStateMachine().changeState(Wait.instance);
			 }
			}
	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#exit(java.lang.Object)
	 */
	public void exit(TeamPlayer player) {
		  player.getTactica().setSupportingPlayer(null);
		  player.getSteering().seekOff();
	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#onMessage(java.lang.Object, com.chidotronics.javacup.fsm.state.Telegram)
	 */
	public boolean onMessage(TeamPlayer o, Telegram telegram) {
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return this.name.hashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return this.name;
	}
}
